package com.steven.ourcontest.information.api

import com.steven.ourcontest.information.bean.IdentifyStatusResponse
import com.steven.ourcontest.information.bean.ManageUsersDetailsRes
import com.steven.ourcontest.information.bean.SchoolList
import com.steven.ourcontest.information.bean.UserNameResponse
import com.steven.ourcontest.information.bean.UserDetail
import com.steven.ourcontest.login.helper.LoginController
import com.steven.ourcontest.me.bean.UsersDetailsRes
import com.steven.ourcontest.net.BaseResponse
import com.steven.ourcontest.net.EmptyResponse
import com.steven.ourcontest.net.NetConstants
import io.reactivex.Observable
import okhttp3.FormBody
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Field
import retrofit2.http.Part
import java.util.concurrent.TimeUnit

class InfoApi private constructor() {
    private val mRetrofit: Retrofit
    private val mApiService: InfoApiService

    companion object {
        const val DEFAULT_TIMEOUT = 10
        val instance: InfoApi by lazy {
            InfoApi()
        }
    }

    init {
        val builder = OkHttpClient.Builder()
        builder.connectTimeout(
            DEFAULT_TIMEOUT.toLong(),
            TimeUnit.SECONDS
        )
        builder.addInterceptor {
            val request = it.request()
            val requestBuilder = request.newBuilder()
            requestBuilder.header("uid", LoginController.userId.toString())
                .header("userType", LoginController.userType)
            return@addInterceptor it.proceed(requestBuilder.build())
        }
        mRetrofit = Retrofit.Builder()
            .client(builder.build())
            .baseUrl(NetConstants.IDENTIFY_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
        mApiService = mRetrofit.create(InfoApiService::class.java)
    }

    fun querySchoolList(schoolName: String): Observable<BaseResponse<SchoolList>> {
        return mApiService.querySchoolList(schoolName)
    }

    fun addUserDetail(
        uid: Long,
        uname: String,
        school: String,
        college: String,
        judge: String,
        startYear: Long,
        schoolType: String,
        userType: String
    ): Observable<BaseResponse<EmptyResponse>> {
        return mApiService.addUserDetail(
            uid,
            uname,
            school,
            college,
            judge,
            startYear,
            schoolType,
            userType
        )
    }

    fun queryIdentifyStatus(uid: Long): Observable<BaseResponse<IdentifyStatusResponse>> {
        return mApiService.queryIdentifyStatus(uid)
    }

    fun uploadCard(body: RequestBody): Observable<BaseResponse<EmptyResponse>> {
        return mApiService.uploadCard(body)
    }

    fun queryUserDetail(): Observable<BaseResponse<UserNameResponse>> {
        return mApiService.queryUserDetail()
    }

    fun queryToBeConfirm(): Observable<BaseResponse<ManageUsersDetailsRes>> {
        return mApiService.queryToBeConfirm()
    }

    /**
     * @param status is default(0) or refuse(1) or pass(2)
     */
    fun updateIdentifyStatus(normalUser: Long, canPass: Boolean): Observable<BaseResponse<EmptyResponse>> {
        return mApiService.updateIdentifyStatus(normalUser, canPass)
    }
}